package com.obtk.shilipai.controller;

import com.obtk.shilipai.entity.Dishe;
import com.obtk.shilipai.entity.JsonResult;
import com.obtk.shilipai.entity.Shop;
import com.obtk.shilipai.entity.ShopType;
import com.obtk.shilipai.service.ShopService;
import com.obtk.shilipai.utils.Md5Utils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Controller
public class ShopController {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ShopService shopService;
//    @Autowired

    /**
     * 默认进入登录页
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "login";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/reg")
    public String reg() {
        return "reg";
    }

    /**
     * 登录后主界面
     *
     * @return
     */
    @RequestMapping("/main")
    public String main() {
        return "main.html";
    }

    /**
     * 后台首页
     *
     * @return
     */
    @RequestMapping("/home")
    public String home() {
        return "home.html";
    }

    /**
     * 用户登录
     *
     * @param loginShop
     * @param session
     * @return
     */
    @RequestMapping("/loginShop")
    @ResponseBody
    public JsonResult login(Shop loginShop, HttpSession session,@RequestParam(defaultValue = "0",required = false) Integer typeId) {
        List<ShopType> shopType = shopService.getShopType(typeId);
        System.out.println(shopType);
        //将接收到的密码 MD5加密  与数据库校对
        loginShop.setShopPassword(Md5Utils.MD5Encode(loginShop.getShopPassword(), "utf-8", false));
        Shop shop = shopService.login(loginShop);
        System.out.println(shop);
        //点击网址首页或logo跳转到首页
        if (shop != null && shop.getStatus()!=0) {
            session.setAttribute("user", shop);
            session.setAttribute("shopType", shopType);
            session.setAttribute("shopName", shop.getShopName());
            return new JsonResult(0, "登录成功");
        }
        if (shop.getStatus()==0) {
            session.setAttribute("shop", shop);
            session.setAttribute("shopType", shopType);
            return new JsonResult(2, "未填写资料");
        }
        return new JsonResult(1, "登录失败");

    }
    @RequestMapping("/setShopInfo")
    public String setShopInfo(Model model){
        HttpSession httpSession=request.getSession();
        Shop user = (Shop) httpSession.getAttribute("shop");
        List<ShopType> shopType = (List<ShopType>) httpSession.getAttribute("shopType");
        model.addAttribute("shop",user);
        model.addAttribute("shopType",shopType);
        return "shopInfo";
    }

    @RequestMapping("/getShopType")
    @ResponseBody
    public List<ShopType> getShopType(Integer typeId){
        List<ShopType> shopType = shopService.getShopType(typeId);
        System.out.println("type"+shopType);
        return shopType;
    };

    @RequestMapping("/getShopByAll")
    @ResponseBody
    public Shop getShopByAll(Integer shopId){
        System.out.println(shopId);
        Shop shop = shopService.getShopByAll(shopId);
        return shop;
    }

    /**
     * 用户退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login";
    }

    @RequestMapping("/user")
    public String User(Model model,Integer shopId){
        HttpSession httpSession=request.getSession();
        Shop shop = shopService.getShopByAll(shopId);
        model.addAttribute("shop",shop);
        model.addAttribute("shopId",shop.getShopId());
        List<ShopType> shopType = (List<ShopType>) httpSession.getAttribute("shopType");
        model.addAttribute("shopType",shopType);
        return "user/list";
    }
    @RequestMapping("/selectUser")
    @ResponseBody
    public String selectUser(String shopUser){
        Integer s = shopService.selectUser(shopUser);
        return s.toString();
    }

    @RequestMapping("/addShop")
    @ResponseBody
    public String addShop(Shop shop){
        shop.setShopPassword(Md5Utils.MD5Encode(shop.getShopPassword(), "utf-8", false));
        // 创建SimpleDateFormat对象，指定时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Date now = new Date();
        // 格式化时间为用“-”隔开的字符串
        String date = dateFormat.format(now);
        shop.setRegTime(date);
        System.out.println("date:"+date);
        Integer s = shopService.addShop(shop);

        return s.toString();
    }

    @RequestMapping("/updateShop")
    @ResponseBody
    public String updateShop(Shop shop){
        Integer s = shopService.updateShop(shop);
        if (s>0){
            String directoryPath = "C:/Users/T/Desktop/shilipai/src/main/resources/static/shop/"+shop.getShopName();
            File directory = new File(directoryPath);
            if (!directory.exists()) {
                boolean success = directory.mkdirs();
                if (success) {
                    System.out.println("目录已创建");
                } else {
                    System.out.println("目录创建失败");
                }
            } else {
                System.out.println("目录已存在");
            }
        }
        return s.toString();
    }

    @RequestMapping("/updateShopImg")
    @ResponseBody
    public String updateShopImg(@RequestParam(value = "file") MultipartFile file,@ModelAttribute("shop")Shop shop) throws IOException {
        if (file!=null){
            HttpSession httpSession=request.getSession();
            String shopName = (String)httpSession.getAttribute("shopName");
            String originalFilename = file.getOriginalFilename();
            // 拼接保存文件的完整路径
            String filePath = "C:/Users/T/Desktop/shilipai/src/main/resources/static/shop/"+shopName+"/" + originalFilename;
            File dest = new File(filePath);
            if (!dest.exists()) {
                // 保存文件到指定路径
                file.transferTo(new File(filePath));
            }else {
                System.out.println("头像文件已存在");
            }
        }
        // 保存文件到指定路径
        String s = shopService.updateShopImage(shop).toString();
        System.out.println("shop---"+shop);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/selectShopName")
    @ResponseBody
    public String selectShopName(String shopName){
        Integer s = shopService.selectShopName(shopName);
        System.out.println(s);
        return s.toString();
    }

}
